package com.chainyoung.component_tmperson.ui.activity.hdwallet;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.chainyoung.common.appcommon.entity.HdWallet;
import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.appcommon.entity.HdWalletTokenCache;
import com.chainyoung.common.appcommon.entity.HdWallets;
import com.chainyoung.common.appcommon.hdwallet.WalletStoreManger;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.ConvertUtils;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.entity.PurseAddr;
import com.chainyoung.component_tmperson.qrcodeview.QRCodeUtil;
import com.chainyoung.component_tmperson.zxing.WriterException;

import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;

import static com.chainyoung.common.appcommon.hdwallet.CoinConstant.ETH;

/**
 * 本地钱包 地址二维码封装的是地址
 */

public class HdWalletCodeActivity extends BaseActivity {


    @BindView(R2.id.tv_purse_addr)
    TextView tvPurseAddr;
    @BindView(R2.id.copy)
    TextView copy;
    @BindView(R2.id.tv_bit_type)
    TextView tvBitType;
    @BindView(R2.id.iv_code)
    ImageView ivCode;
    @BindView(R2.id.txt_addr)
    TextView txtAddr;

    private String coinName;
    private String coinCnName;

    //剪切板管理工具类
    private ClipboardManager mClipboardManager;
    //剪切板Data对象
    private ClipData mClipData;

    private String coinTotalName;

    private String logo;

    // private HdWallets hdWallets;
    // private HdWallet hdWallet = null;

    private HdWalletCache hdWallet;
    private HdWalletTokenCache hdWalletTokenCache;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected void initUI() {
        hdWallet = (HdWalletCache) getIntent().getSerializableExtra("hdWallet");
        hdWalletTokenCache = (HdWalletTokenCache) getIntent().getSerializableExtra("hdWalletToken");
        setCurrentTitle(hdWalletTokenCache == null ? hdWallet.getCoinType() + "/" + hdWallet.getCnName() + "收款地址" :
                hdWalletTokenCache.getTokenSymbol() + "收款地址");
        tvBitType.setText(hdWalletTokenCache == null ? String.format(getString(R.string.wallet_tips), hdWallet.getCoinType() + "/" + hdWallet.getCnName()) :
                String.format(getString(R.string.wallet_tips), hdWalletTokenCache.getTokenSymbol()));


       /* hdWallets = WalletStoreManger.getHdWallets();
        try {
            hdWallet = hdWallets.getWallets().get(0).getWallets().get(0);
        } catch (Exception ignored) {

        }
        if (hdWallet != null) {
            setCurrentTitle("ETH/以太坊地址");
            tvBitType.setText(String.format(getString(R.string.wallet_tips), "ETH/以太坊"));
        }*/

        mClipboardManager = (ClipboardManager) mContext.getSystemService(mContext.CLIPBOARD_SERVICE);

        if (hdWallet != null) {
            generateQrcode();
            tvPurseAddr.setText(hdWallet.getAddress());
        }

        txtAddr.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {

    }


    private void generateQrcode() {
        try {
            String codeText = "local" + hdWallet.getCoinType().toLowerCase() + ":" + hdWallet.getAddress();
            int logo = R.mipmap.icon_eth;
            BitmapDrawable bd = (BitmapDrawable) ContextCompat.getDrawable(mContext, logo);
            Bitmap bitmap = bd.getBitmap();
            int width = ConvertUtils.dp2px(mContext, 150);
            Bitmap bm = QRCodeUtil.createQRCode(codeText, bitmap, width, width);
            ivCode.setImageBitmap(bm);
            ivCode.setScaleType(ImageView.ScaleType.FIT_XY);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_charge;
    }


    @OnClick({R2.id.copy})
    void onClick(View view) {
        if (view.getId() == R.id.copy) {
            String text = tvPurseAddr.getText().toString();
            //创建一个新的文本clip对象
            mClipData = ClipData.newPlainText("block", text);
            //把clip对象放在剪贴板中
            mClipboardManager.setPrimaryClip(mClipData);
            ToastUtil.showToast(getString(R.string.tmperson_addr_copy_success));
        }
    }


}
